//
// Created by ct on 2020/11/30.
//

#ifndef SEALUCIA_ROUTER_H
#define SEALUCIA_ROUTER_H


#include <string>
#include <vector>
#include <functional>
#include <memory>
#include "url_parse.h"

namespace router {

    // controll 层函数，map 为解析的url
    typedef std::function<std::string(url_parse::url_result_map)> handler_t;

    // 404
    std::string not_found(const url_parse::url_result_map& map);

    typedef struct _tree_node {
        std::string                                     path;
        std::string                                     indices;
        std::vector<std::shared_ptr<struct _tree_node>> children;
        handler_t                                       handle;
    }tree_node_t;

    class node {
    public:

        //! ctor
        node();

        //! dtor
        ~node();

        //! 拷贝构造函数
        node(const node&) = default;

        //! 赋值运算符
        node& operator=(const node&) = delete;

        //! addRouter向路径添加具有给定句柄的节点
        //! 不是并发安全的！
        void addRoute( std::string path,  const handler_t& handle);

        //! 获取路径处理程序
        handler_t handler(std::string path);

    private:
        void insertChild(tree_node_t* node, std::string& path, handler_t handle);

    private:
        std::shared_ptr<tree_node_t> node_;
    };


} // namespace router


#endif //SEALUCIA_ROUTER_H
